namespace BM.Tools.WebControls.MForm.Controls.Helper
{
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using BM.Tools.WebControls.MForm.Controls.Items;
    using BM.Tools.WebControls.MForm.Generator;
    using BM.Tools.WebControls.MForm.Resources;

    /// <summary>
    /// A ChoiceBox is a <see cref="ListBox" /> control that is specific for the <see cref="ChoiceItem" /> object.
    /// It displays the available choices, and on choice change it shows only the specified item and hides all others
    /// </summary>
    public class ChoiceBox : ListBox
    {
        private ChoiceItem owner;

        /// <summary>
        /// Initializes a new instance of the <see cref="ChoiceBox"/> class. 
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public ChoiceBox(ChoiceItem owner)
        {
            Owner = owner;
            Initialize();
        }

        /// <summary>
        /// Gets Owner.
        /// </summary>
        /// <value>
        /// The owner.
        /// </value>
        public ChoiceItem Owner
        {
            get
            {
                return owner;
            }

            private set
            {
                owner = value;
            }
        }

        /// <summary>
        /// Gets or sets SelectedValue.
        /// </summary>
        /// <value>
        /// The selected value.
        /// </value>
        public override string SelectedValue
        {
            get
            {
                return base.SelectedValue;
            }

            set
            {
                EnsureChildControls();
                base.SelectedValue = value;                
            }
        }

        /// <summary>
        /// Gets Visible.
        /// </summary>
        /// <value>
        /// The visible.
        /// </value>
        public override bool Visible
        {
            get
            {
                if (Owner.Mode == FormBuilderMode.View)
                {
                    return false;
                }

                return base.Visible;
            }
        }

        /// <summary>
        /// Gets the collection of items in the list control.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Web.UI.WebControls.ListItemCollection"/> that represents the items within the list. The default is an empty list.
        /// </returns>
        public override ListItemCollection Items
        {
            get
            {
                EnsureChildControls();
                return base.Items;
            }
        }

        /// <summary>
        /// Adds onchange attribute, providing client interactivity functionality
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute("onchange", "ChoiceItem.choose(this);");            
        }

        /// <summary>
        /// Adds all list items to the control
        /// </summary>
        protected override void CreateChildControls()
        {
            Items.Clear();
            Items.Add(new ListItem(LanguageResource.Choose, string.Empty));
            foreach (Offspring child in Owner.Offsprings)
            {
                Items.Add(new ListItem(child.DisplayName, child.Name));
            }
        }

        private void Initialize()
        {
            EnableViewState = false;
            CssClass = ChoiceItem.ChoiceItemCssClass;
            Rows = 1;
        }
    }
}